package jzbank.service.impl;

import jzbank.common.exception.SyncException;
import jzbank.dao.serializabledao.DataExportDao;
import jzbank.dao.serializabledao.DatabaseConfigDao;
import jzbank.pojo.comfig.DatabasePO;
import jzbank.pojo.dto.DataExportDTO;
import jzbank.service.DatabaseConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class DatabaseConfigServiceImpl implements DatabaseConfigService {

    @Autowired
    private DatabaseConfigDao databaseConfigDao;

    @Autowired
    private DataExportDao dataExportDao;

    @Override
    public void createDatabaseConfig(DatabasePO databasePO) {
        databasePO.setDbId(UUID.randomUUID().toString());//生产UUID
        databaseConfigDao.addDatabasePo(databasePO);
    }

    @Override
    public void deleteDatabaseConfigById(String id) {
        List<DataExportDTO> allDataExport = dataExportDao.getAllDataExport();
        for (DataExportDTO dataExportDTO : allDataExport) {
            if (dataExportDTO.getDbId().equals(id)){
                throw new SyncException("这个配置信息"+dataExportDTO.getTaskName()+"任务正在使用，不允许删除");
            }
        }
        databaseConfigDao.deleteDatabasePo(id);
    }

    @Override
    public DatabasePO selectDatabaseConfigById(String id) {
        return databaseConfigDao.getDatabasePoByDbId(id);
    }

    @Override
    public List<DatabasePO> selectAllDatabaseConfig() {
        return databaseConfigDao.getAllDatabasePo();
    }

    @Override
    public void updateDatabaseConfig(DatabasePO databasePO) {
        databaseConfigDao.updateDatabasePo(databasePO);
    }
}